package com.gao.d_routing;

import com.gao.App;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author gao
 * @time 2022/07/05 10:57:47
 */
@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class Producer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void test() throws InterruptedException {
        for (int i = 1; i <= 10; i++) {
            String rk = getSeverity();
            String msg = "Andy" + i;
            System.out.println("投递：" + msg + ", 级别：" + rk);
            rabbitTemplate.convertAndSend("direct_logs", rk, msg);
        }

        TimeUnit.SECONDS.sleep(3);
    }

    private static String getSeverity() {
        int r = new Random().nextInt(3);
        String severity = null;
        switch (r) {
            case 0:
                severity = "info";
                break;
            case 1:
                severity = "warning";
                break;
            case 2:
                severity = "error";
                break;
            default:
        }
        return severity;
    }
}
